<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">交易管理</a></li>
        <li class="active">订单管理</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="selectForm" class="form-inline" th:action="@{/order/list}" role="form">

            <div class="form-group">
                <label class="control-label" for="outTradeNo">订单编号：</label>
                <div class="control-inline">
                    <input type="text" id="outTradeNo" name="outTradeNo" class="form-control" />
                </div>
            </div>

            <div class="form-group">
                <label class="control-label" for="mchId">商户编号：</label>
                <div class="control-inline">
                    <input type="text" id="mchId" name="mchId" class="form-control width-90" />
                </div>
            </div>

            <div class="form-group">
                <label class="control-label" for="tradeStatus">交易状态：</label>
                <select id="tradeStatus" name="tradeStatus" class="form-control">
                    <option value="">==请选择==</option>
                    <option value="TRADE_FAILE">订单失效</option>
                    <option value="WAIT_BUYER_PAY">等待支付</option>
                    <option value="TRADE_CLOSED">订单关闭</option>
                    <option value="TRADE_SUCCESS">交易成功</option>
                    <option value="TRADE_FINISHED">交易完成</option>
                </select>
            </div>

            <div class="form-group">
                <label class="control-label" for="serviceType">交易类型：</label>
                <select id="serviceType" name="serviceType" class="form-control">
                    <option value="">交易类型</option>
                    <option th:each="list:${tradeList}" th:value="${list.service}"
                            th:text="${list.service}"></option>
                </select>
            </div>

            <div class="form-group">
                <label class="control-label">交易时间：</label>
                <input type="text" class="form-control" name="createTimeString" id="startDate"
                        onclick="WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd',onpicked:function(){endDate.click();}})" />
                --
                <input type="text" class="form-control" name="endTimeString" id="endDate"
                       onclick="WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd'})" />

                <div class="time-select">
                    <a class="yestoday" href="javascript:;">昨天</a>
                    <a class="today on" href="javascript:;">今天</a>
                    <a class="one-week" href="javascript:;">近一周</a>
                </div>
            </div>

            <div class="form-group">
                <label class="control-label" for="totalFee">交易金额：</label>
                <input type="text" id="totalFee" name="totalFee" class="form-control" />
            </div>

            <div class="form-group">
                <label class="control-label" for="srcOrderNo">上游订单号：</label>
                <div class="control-inline">
                    <input type="text" id="srcOrderNo" name="srcOrderNo" class="form-control" />
                </div>
            </div>

            <div class="form-group">
                <label class="control-label" for="srcMchId">上游商户号：</label>
                <div class="control-inline">
                    <input type="text" id="srcMchId" name="srcMchId" class="form-control" />
                </div>
            </div>
                <shiro:hasPermission name="order:list">
                    <div class="btn-group" style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-info">查询</button>
                        <button type="reset" id="btn_reset" class="btn btn-warning">重置</button>
                    </div>
                    <div class="" style="text-align: left;">
                        <span><input id="refreshOnTime" name="refreshOnTime" type="checkbox" value="1" /> 是否自动刷新</span>
                    </div>

                </shiro:hasPermission>
        </form>
    </div>
    <div class="jax-box jax-box-table">

        <div id="toolbar" class="toolbar btn-group">
            <!--<shiro:hasPermission name="agent:add">-->
                <button id="export-btn" type="button" class="btn btn-primary">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>导出订单
                </button>
            <!--</shiro:hasPermission>-->

        </div>

        <div class="wrapper-content">
            <div class="ibox-content animated fadeInRight">
                <div class="jqGrid_wrapper">
                    <table id="table_list"></table>
                    <div id="pager_list"></div>
                </div>
            </div>
        </div>

    </div>
</section>

<script type="text/javascript">
    $(document).ready(function () {

        var retryNotifyFlag = "[[${@perms.hasPerm('order:retryNotify')}]]";

        $("#startDate").val(getNowFormatDate(0));
        $("#endDate").val(getNowFormatDate(0));

        var dataGrid = new DataGrid({
            dataGrid: $('#table_list'),
            searchForm: $('#selectForm'),
            dataGridPage: $('#pager_list'),

            rownumbers: true,
            rownumWidth: 60,
            colModel: [
                {
                    header: '代理商名称',
                    name: 'agentName',
                    index: 'agentName',
                    editable: false,
                    width: 60,
                    sortable: false,
                    search: false
                },
                {
                    header: '商户流水号',
                    name: 'outTradeNo',
                    index: 'outTradeNo',
                    width: 60,
                    align: 'left',
                    sortable: false
                },
                {
                    header: '交易金额',
                    name: 'totalFee',
                    index: 'totalFee',
                    sortable: false, formatter: moneyFormat,
                    width: 35
                },
                {
                    header: '商户金额',
                    name: 'profit',
                    index: 'profit',
                    sortable: false, formatter: moneyFormat,
                    width: 35
                },
                {
                    header: '交易状态',
                    name: 'tradeStatus',
                    index: 'tradeStatus',
                    editable: true,
                    width: 40,
                    sortable: false,
                    formatter: function (value) {
                        switch (value) {
                            case 'TRADE_FAILE':
                                return '订单失效';
                            case 'WAIT_BUYER_PAY':
                                return '等待支付';
                            case 'TRADE_CLOSED':
                                return '订单关闭';
                            case 'TRADE_SUCCESS':
                                return '交易成功';
                            case 'TRADE_FINISHED':
                                return '交易完成';
                            default:
                                return "订单失效";
                        }
                    }
                },
                {
                    header: '交易类型',
                    name: 'serviceType',
                    index: 'serviceType',
                    editable: true,
                    width: 60,
                    sortable: false
                },
                {
                    header: '上游商户号',
                    name: 'srcMchId',
                    index: 'srcMchId',
                    width: 60,
                    align: 'left',
                    sortable: false
                },
                {
                    header: '上游订单号',
                    name: 'srcOrderNo',
                    index: 'srcOrderNo',
                    width: 60,
                    align: 'left',
                    sortable: false
                },
                {
                    header: '上游返回信息',
                    name: 'srcReason',
                    index: 'srcReason',
                    editable: true,
                    width: 40,
                    sortable: false
                },
                {
                    header: '创建时间',
                    name: 'createTime',
                    index: 'createTime',
                    editable: false,
                    sortable: false,
                    width: 60
                },
                {
                    header: '支付时间',
                    name: 'payTime',
                    index: 'payTime',
                    editable: true,
                    sortable: false,
                    width: 60
                },
                {
                    header: '操作',
                    label: 'operate',
                    name: '',
                    index: 'operate',
                    width: 50,
                    align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        // var retry = retryNotifyFlag == "true" ? '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="retryNotify(\'' + rowObject.outTradeNo + '\')">补单</a>' : '';
                        var retry = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="retryNotify(\'' + rowObject.outTradeNo + '\')">补单</a>';
                        return retry;
                    }

                }
            ],
            footerrow: true, //分页上添加一行，用于显示统计信息
            userDataOnFooter: true,
            altRows: true
        });

        $("#btn_query").click(function () {
            $("#table_list").jqGrid('setGridParam', {
                datatype: 'json',
                postData: {
                    outTradeNo: $('input[name=outTradeNo]').val(),
                    mchId: $('input[name=mchId]').val(),
                    tradeStatus: $('select[name=tradeStatus]').val(),
                    createTimeString: $('input[name=createTimeString]').val(),
                    endTimeString: $('input[name=endTimeString]').val(),
                    totalFee: $('input[name=totalFee]').val(),
                    serviceType: $('select[name=serviceType]').val(),
                    srcMchId: $('input[name=srcMchId]').val(),
                    srcOrderNo: $('input[name=srcOrderNo]').val()
                }, // 发送数据
                page: 1
            }).trigger("reloadGrid"); //重新载入
        });

        $("#export-btn").click(function () {

            DownLoadFile({

                url: ctxPath + "/order/exportExcle",
                data: {
                    outTradeNo: $('input[name=outTradeNo]').val(),
                    mchId: $('input[name=mchId]').val(),
                    tradeStatus: $('select[name=tradeStatus]').val(),
                    createTimeString: $('input[name=createTimeString]').val(),
                    endTimeString: $('input[name=endTimeString]').val(),
                    totalFee: $('input[name=totalFee]').val(),
                    serviceType: $('select[name=serviceType]').val(),
                    srcMchId: $('input[name=srcMchId]').val(),
                    srcOrderNo: $('input[name=srcOrderNo]').val()
                }//要发送的数据

            });
        });

        $("input[name='refreshOnTime']").change(function() {

            if ($(this).is(':checked')) {
                layer.msg("已开启自动刷新！");

                $('body').everyTime('5s', 'orderRefresh', function() {
                    $("#table_list").jqGrid('setGridParam', {
                        datatype: 'json',
                        postData: {
                            outTradeNo: $('input[name=outTradeNo]').val(),
                            mchId: $('input[name=mchId]').val(),
                            tradeStatus: $('select[name=tradeStatus]').val(),
                            createTimeString: $('input[name=createTimeString]').val(),
                            endTimeString: $('input[name=endTimeString]').val(),
                            totalFee: $('input[name=totalFee]').val(),
                            serviceType: $('select[name=serviceType]').val(),
                            srcMchId: $('input[name=srcMchId]').val(),
                            srcOrderNo: $('input[name=srcOrderNo]').val()
                        }, // 发送数据
                        page: 1
                    }).trigger("reloadGrid"); //重新载入

                });
            } else {
                layer.msg('已关闭自动刷新');
                $('body').stopTime ();
            }
        });

        // $(".time-select a").click(function () {
        //
        // });

        $('.time-select').find('a').click(function(){
            $(this).addClass('on').siblings('a').removeClass('on');
        })

        $(".today").click(function(){
            $("#startDate").val(getNowFormatDate(0));
            $("#endDate").val(getNowFormatDate(0));
        })

        $(".yestoday").click(function(){
            $("#startDate").val(getNowFormatDate(1));
            $("#endDate").val(getNowFormatDate(1));
        })

        $(".one-week").click(function(){
            $("#startDate").val(getNowFormatDate(6));
            $("#endDate").val(getNowFormatDate(0));
        })


    });

    /** 补发订单回调 **/
    function retryNotify(outTradeNo) {
        var lock = false; // 默认未锁定
        if (!lock) {
            lock = true;
            $.ajax({
                dataType: "json",
                type: "GET",
                url: ctxPath + "/order/retryNotify?outTradeNo="+outTradeNo,
                success: function (data) {
                    if (200 == data.code) {
                        layer.alert(data.msg, function () {
                            //清除页面数据
                            layer.closeAll();
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                },
                error: function () {
                    layer.alert("操作请求错误，请您稍后再试", function () {
                        layer.closeAll();
                        //加载load方法
                        //load(obj);//自定义
                    });
                }
            });
        }
    }


    /*获取当前日期*/
    function getNowFormatDate(datas) {
        var newdate;
        var date = new Date();
        var year=date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        var marchDay;
        if((year%4==0 && year%100!=0)||(year%100==0 && year%400==0)){
            marchDay=29;
        }else{
            marchDay=28;
        }

        switch(month){
            case 1 :
                if(strDate-datas<=0){
                    newdate=strDate+31-datas;
                    year=year-1;
                    month=12;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 2:
                if(strDate-datas<=0){
                    newdate=strDate+31-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 3:
                if(strDate-datas<=0){
                    newdate=strDate+marchDay-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 4:
                if(strDate-datas<=0){
                    newdate=strDate+31-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 5:
                if(strDate-datas<=0){
                    newdate=strDate+30-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 6:
                if(strDate-datas<=0){
                    newdate=strDate+31-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 7 :
                if(strDate-datas<=0){
                    newdate=strDate+30-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 8:
                if(strDate-datas<=0){
                    newdate=strDate+31-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 9:
                if(strDate-datas<=0){
                    newdate=strDate+31-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 10:
                if(strDate-datas<=0){
                    newdate=strDate+30-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 11 :
                if(strDate-datas<=0){
                    newdate=strDate+31-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;
            case 12 :
                if(strDate-datas<=0){
                    newdate=strDate+30-datas;
                    month=month-1;
                }else{
                    newdate=strDate-datas;
                };
                break;


        }

        month=month<=9? "0" + month:month;
        newdate=newdate<=9?"0"+newdate:newdate;
        var currentdate = year + "-" + month + "-"+ newdate
        return currentdate;


    }

    function moneyFormat(cellValue) {
        if (cellValue == null || cellValue == '') {
            cellValue = 0;
        }
        cellValue = cellValue.toFixed(2);
        var total_p = "￥" + cellValue;

        return total_p;
    }
</script>